//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using EntLibContrib.Query.Configuration;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query.Parameters
{
	/// <summary>
	/// Type to hold the elements of a single DataSet query parameter
	/// </summary>
	/// <remarks>Note that parameter values for this type of parameter come from the dataset and
	/// not the base parameter 'value' property</remarks>
	[ConfigurationElementType(typeof(DataSetParameterData))]
	public class DataSetParameter : DataParameter
	{
		#region Fields
		private string column;
		private DataRowVersion version;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the source column.
		/// </summary>
		/// <value>The source column.</value>
		public string Column
		{
			get { return column; }
			set { column = value; }
		}

		/// <summary>
		/// Gets or sets the source version.
		/// </summary>
		/// <value>The source version.</value>
		/// <remarks>
		/// Versions are Current, Default, Original and Proposed.
		/// </remarks>
		public DataRowVersion Version
		{
			get { return version; }
			set { version = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="DataSetParameter"/> class.
		/// </summary>
		/// <param name="name">The parameter name.</param>
		public DataSetParameter(string name)
			: this(name, DbType.Guid, ParameterDirection.Input, string.Empty, DataRowVersion.Current)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DataSetParameter"/> class.
		/// </summary>
		/// <param name="name">The parameter name.</param>
		/// <param name="type">The parameter type.</param>
		public DataSetParameter(string name, DbType type)
			: this(name, type, ParameterDirection.Input, string.Empty, DataRowVersion.Current)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DataSetParameter"/> class.
		/// </summary>
		/// <param name="name">The parameter name.</param>
		/// <param name="type">The parameter type.</param>
		/// <param name="direction">The direction of the parameter relative to the data source.</param>
		public DataSetParameter(
			string name,
			DbType type,
			ParameterDirection direction
			)
			: this(name, type, direction, string.Empty, DataRowVersion.Current)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DataSetParameter"/> class.
		/// </summary>
		/// <param name="name">The parameter name.</param>
		/// <param name="type">The parameter type.</param>
		/// <param name="direction">The direction of the parameter relative to the DataSet.</param>
		/// <param name="column">The dataset column name.</param>
		public DataSetParameter(
			string name,
			DbType type,
			ParameterDirection direction,
			string column
			)
			: this(name, type, direction, column, DataRowVersion.Current)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DataSetParameter"/> class.
		/// </summary>
		/// <param name="name">The parameter name.</param>
		/// <param name="type">The parameter type.</param>
		/// <param name="direction">The direction of the parameter relative to the DataSet.</param>
		/// <param name="column">The source column.</param>
		/// <param name="version">The source version.</param>
		public DataSetParameter(
			string name,
			DbType type,
			ParameterDirection direction,
			string column,
			DataRowVersion version
			)
			: base(name, type, direction)
		{
			ValidateAndStoreParameters(column, version);
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Validates the and store parameters.
		/// </summary>
		/// <param name="dataSetColumn">The data set column.</param>
		/// <param name="dataSetVersion">The data set version.</param>
		private void ValidateAndStoreParameters(string dataSetColumn, DataRowVersion dataSetVersion)
		{
			// check for a null or empty column
			if (string.IsNullOrEmpty(dataSetColumn))
				throw new ArgumentNullException("dataSetColumn", InternalResources.ExceptionDataSetColumnNullOrEmpty);

			// store the parameters
			this.column = dataSetColumn;
			this.version = dataSetVersion;
		}
		#endregion
	}
}
